package com.atguigu.shirolearn.controller;



import com.atguigu.shirolearn.bean.User;
import com.atguigu.shirolearn.service.UserService;
import com.atguigu.shirolearn.utils.CookieUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.SoundbankResource;

@Controller
public class UserController{


    @Autowired
    UserService userService;


    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        System.out.println("UserController.hello()");

        return "ok";
    }


    @RequestMapping("/testThymeleaf")
    public String testThymeleaf(Model model){
        //把数据存入model
        model.addAttribute("name", "黑马程序员");
        //返回test.html
        return "test";
    }

    @RequestMapping("/")
    public String index(Model model){
        //把数据存入model
        model.addAttribute("name", "黑马程序员");
        //返回test.html
        return "redirect:tologin";
    }

    @RequestMapping("/add")
    public String add(ModelMap modelMap){

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        modelMap.addAttribute("msg", user.getName());
        //返回添加用户页面
        return "add";
    }

    //跳转登录页面
    @RequestMapping("/tologin")
    public String tologin(HttpServletRequest req){

        /*
        获取session，若有session，则直接登录
            但是有可能session还没删除，用户已经删除【业务中不涉及删除用户】
         */
        String userName = CookieUtil.getCookieValue(req, "user", false);
        if (StringUtils.isBlank(userName)) {
            //如果用户不存在
            return "login";
        }else{
            User user = userService.getUserByName(userName);
            //1.获取subject
            Subject subject = SecurityUtils.getSubject();
            //2.封装用户数据
            UsernamePasswordToken token = new UsernamePasswordToken(user.getName(), user.getPassword());
            subject.login(token);
            System.out.println("他妈");
            return "redirect:/testThymeleaf";
        }
        //返回添加用户页面

    }

    /**
     * 处理用户登录逻辑
     * @param name 用户名
     * @param password 密码
     * @param model
     * @return
     */
    @RequestMapping("/login")
    public String login(String name, String password, Model model, HttpServletRequest req, HttpServletResponse resp){



        /*
        使用shiro编写认证操作
         */

        //1.获取subject
        Subject subject = SecurityUtils.getSubject();
        //2.封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);

        //3。执行登录方法
        try {
            subject.login(token);
            //若登录成功，设置cookie
            CookieUtil.setCookie(req, resp, "user", name, 60*60, false);
            System.out.println("设置cookie成功");
            //若登录成功，跳转到test页面
            return "redirect:/testThymeleaf";
        } catch (UnknownAccountException e) {
            //若用户名不存在
            model.addAttribute("msg", "用户名不存在");
            return "login";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码错误");
            return "login";
        }

        //返回添加用户页面

    }



    @RequestMapping("/update")
    public String update(){

        //返回更新用户页面
        return "update";
    }

    @RequestMapping("/noAuth")
    public String noAuth(){

        //返回更新用户页面
        return "noAuth";
    }
}
